<?php

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Auth;
use Request;
use Validator;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller

{
    /**
     * 登录首页
     */
    public function index()
    {
        if (IS_POST) {
            //登录验证
            //验证验证码
            $rules = ['captcha' => 'required|captcha'];
            $validator = Validator::make(['captcha' => Request()->post('captcha')], $rules);
            if ($validator->fails()) {
                return redirect('/admin/login')->with('status', '验证码错误咯');
            }
            //验证用户名
            $remember = Request()->post('remember') ? true : false;
            $res = Auth::attempt(['username' => Request()->post('username'), 'password' => Request()->post('password')], $remember);
            if ($res) {
                return redirect('/admin')->with('status', '登录成功');
            } else {
                return redirect('/admin/login')->with('status', '密码或账号错误');
            }
        }
        return view('admin.login.index');
    }


    /*退出*/
    public function logout()
    {
        Auth::logout();
        return redirect('/admin/login')->with('status', '退出成功');
    }


}
